<?php

/**
 * @Project NUKEVIET 3.x - GETNEWS
 * @Author ĐẶNG ĐÌNH TỨ (dlinhvan@gmail.com)
 * @Copyright (C) 2010 webdep24.com All rights reserved
 * @Createdate 10/08/2012 08:00
 */

if( ! defined( 'NV_IS_FILE_ADMIN' ) ) die( 'Stop!!!' );
$time_start = microtime( true );
global $urlimages, $urlink, $module, $folder, $global_config, $module_config;
$contents = "";
$folder = date( "Y_m" );
$do = 0;
$catids = "";
$error = "";
$hauto = "24h";
$module = $module_config[$module_name]['module'];
$mod_data = str_replace( '-', '_', $module );
$data = array();
$link_arr = array();
$urlink = "http://hn.24h.com.vn";

$url = filter_text_input( 'cat', 'post', '' );
$do = $nv_Request->get_int( 'do', 'post', 0 );
$catids = $nv_Request->get_int( 'catids', 'post', 0 );
$sourceid = $nv_Request->get_int( 'sourceid', 'post', 0 );
nguontin( $sourceid, $url );
$contents .= "<table class=\"tab1\">
      <thead>
         <tr>
            <td>
               " . $lang_module['info_h24h'] . "
            </td>
         </tr>
      </thead></table>";

$contents .= nv_get_cat( $module, $hauto, $url, $sourceid, $catids );

$contents .= xuat_html( 0 );

// lay thong tin html
$loop = 'div[class="boxDonItem"]';
$title_t = 'div.boxDoiItem-content div.div_title_news a';
$link_t = "div.cate_newsexcerpt p a";
$home_t = "div.cate_newsexcerpt p";
$img_t = "a img";

if( $do )
{
	$id_block_content = array_unique( $nv_Request->get_typed_array( 'bids', 'post', 'int', array() ) );
	$getContent = new UrlGetContents( $global_config );
	$html = $getContent->get( $url );
	$html1 = str_get_html( $html );
	foreach( $html1->find( $loop ) as $e )
	{
		if( $e->find( $title_t, 0 ) ) $title = nv_unhtmlspecialchars( trim( $e->find( $title_t, 0 )->innertext ) );
		$alias = change_alias( $title );
		$data[$alias]['title'] = $title;
		$data[$alias]['alias'] = $alias;

		if( $e->find( $title_t, 0 ) ) $data[$alias]['link'] = $urlink . $e->find( $title_t, 0 )->href;

		if( $e->find( $img_t, 0 ) )
		{
			$img = $e->find( $img_t, 0 )->src;
		}
		if( ! empty( $img ) )
		{
			if( nv_is_url( $img ) ) $data[$alias]['homefile'] = $img;
			else  $data[$alias]['homefile'] = $urlink . $img;
		}
		else  $data[$alias]['homefile'] = "";

	}
	$html1->clear();
	unset( $html1, $html );

	$contents .= xuat_html( 1 ); // xuat thanh  noi dung tieu de
	$ii = 1;
	foreach( $data as $alias => $array )
	{

		$getContent = new UrlGetContents( $global_config );
		$html = $getContent->get( $array['link'] );
		$html1 = str_get_html( $html );
		if( $html1->find( 'h1[class="baiviet-title"]', 0 ) )
		{
			$title = $html1->find( 'h1[class="baiviet-title"]', 0 )->plaintext;
			$array['title'] = trim( nv_htmlspecialchars( $title ) );
			$array['alias'] = change_alias( $title );
		}
		$sql = "SELECT COUNT(*) FROM `" . NV_PREFIXLANG . "_" . $module . "_rows` WHERE `alias`=" . $db->dbescape_string( $array['alias'] );
		$result = $db->sql_query( $sql );
		list( $exist ) = $db->sql_fetchrow( $result );

		if( empty( $exist ) )
		{
			if( ! ( $html1->find( 'div.text-conent', 0 ) ) )
			{
				$error .= "Dữ liệu nguồn bị thay đổi";
				$link_arr[] = $array['link'];
				//break;
			}
			else
			{

				if( $html1->find( 'div.baiviet-head-noidung', 0 ) )
				{
					$hometext = $html1->find( 'div.baiviet-head-noidung', 0 )->plaintext;
					$array['hometext'] = str_replace( "(Tin tuc) - ", "", $hometext );
				}
				else  $array['hometext'] = "";

				$publtime = 0;
				$time = nv_get_content( '<div class="publication_date">', '</div>', $html );
				if( ! empty( $time ) )
				{
					$time = nv_time( nv_conver_utf8( $time ) );
					$emin = substr( $time, 10, 2 ) - 10;
					$ehour = substr( $time, 8, 2 );
					$y = substr( $time, 4, 4 );
					$m = substr( $time, 2, 2 );
					$d = substr( $time, 0, 2 );
					$publtime = mktime( $ehour, $emin, 0, $m, $d, $y );
					if( date( 'Y', $publtime ) == 1970 or date( 'Y', $publtime ) == 1999 )
					{
						$error .= "Lỗi Không Cập Nhật Được Thời Gian";
						break;
					}

				}
				else
				{
					$error .= "Lỗi Không Cập Nhật Được Thời Gian";
					break;
				}

				$body = nv_get_content( '<div class="text-conent">', '<div class="nguontin">', $html );
				$body = preg_replace( '#(href|src)="\s([^:"]*)(?:")#', '$1="$2"', $body );
				$body = url_link( $body, $urlink );
				$body = stripwhitespace( $body );
				$body = explode( '<div class="bl">', $body );
				$body = explode( '<div class="baiviet-tags">', $body[0] );
				$body = substr_replace( $body, "", -6 );
				$bodytext = preg_replace( '@<script[^>]*?.*?</script>@siu', '', $body[0] );
				$bodytext = str_replace( 'onclick="return openNewImage(this, \'\')"', '', $bodytext );

				$bodytext = url_link( $bodytext, $urlink );
				if( $module_config['getnews']['load_image'] == 1 )
				{
					$bodytext = load_image( $bodytext, '' );
				}
				$bodytext = stripwhitespace( $bodytext );

				if( ! empty( $bodytext ) )
				{
					$imgfile = nv_get_images( $bodytext );
					if( ! empty( $imgfile ) and $module_config[$module_name]['image_body'] == 1 )
					{
						$img = explode( "/", $imgfile );
						$imgfile = end( $img );
						$imgfile = NV_UPLOADS_REAL_DIR . "/" . $module . "/" . $folder . "/" . $imgfile;
						if( file_exists( $imgfile ) )
						{
							$img_file = nv_filter( $imgfile );
							$homefile = $img_file[0];
							$homeimgthumb = $img_file[1];
						}
						elseif( ! empty( $array['homefile'] ) )
						{
							$homefile = $array['homefile'];
							$homeimgthumb = "";
						}
						elseif( ! empty( $imgfile ) )
						{
							$homefile = "";
							$homeimgthumb = "";
						}
					}
					elseif( $array['homefile'] != "" )
					{
						$homefile = $array['homefile'];
						$homeimgthumb = "";

					}
					elseif( ! empty( $imgfile ) )
					{
						$homefile = $imgfile;
						$homeimgthumb = "";
					}
					else
					{
						$homefile = "";
						$homeimgthumb = "";
					}

				}
				else
				{
					$error .= "Lỗi chi tiết bài viết trống";
					break;
				}

				if( empty( $error ) )
				{
					nv_save_data( $module, $catids, $sourceid, $array['title'], $array['alias'], $array['hometext'], $publtime, $homefile, $homeimgthumb, $bodytext, $array['link'], $id_block_content );

					$contents .= "<table width=\"100%\" border=\"1\">";
					$contents .= "  
					<tr>
						<td width='5%' style=\"height:26px;\" align=center><strong>$ii</strong></td>
						<td width='61%'><strong><a style=\"padding-left:10px;text-decoration:none;color:black\" href=\"" . $array['link'] . "\" target='_blank'>" . $array['title'] . "<img src=\"" . NV_BASE_SITEURL . "themes/default/images/" . $module_name . "/newnew.gif\"></a></strong></td>
						<td width='34%' align=center style=\"width:100px;\"><strong>" . date( 'H:i, d-m-Y', $publtime ) . "</strong></td>
					</tr>";
					$contents .= "</table>";
				}
				$html1->clear();
				unset( $html1, $html );
			}

		}

		$ii++;
		if( $ii > 10 ) break;
	}
	if( ! empty( $error ) )
	{
		nv_sendmail( array( $global_config['site_name'], $global_config['site_email'] ), $global_config['error_send_email'], "Error get new :" . $error, "Error get new :" . $error . " từ website " . NV_SERVER_NAME . " nguồn " . $url . "  " . implode( "<br />", $link_arr ) . "  " . $global_config['site_email'] );
	}
	else
	{
		optimize();
		nv_del_moduleCache( $module );
	}
}

$time_end = microtime( true );
$time = $time_end - $time_start;
$contents .= "<br>" . $error . "\n";
$contents .= "<br>Thời gian cập nhật: $time giây\n";
include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
include ( NV_ROOTDIR . "/includes/footer.php" );

?>